cc.Class({
    extends: cc.Component,
    properties: {
        ProgressBar:cc.ProgressBar,
        label: cc.Label
    },
    onLoad: function () {
        
        // cc.sys.localStorage.removeItem('token');
        this.initGlobal();
    },

    // 初始化全局变量
    initGlobal(){
        Global.Config = require('../Config');
        Global.Code = require('../Define/Code');
        Global.Utils = require('../Utils/utils');
        Global.Args = Global.Utils.urlParse();
        Global.NoticeManager = require('../Base/NoticeManager');
        
        Global.CCHelper = require('../Utils/CCHelper');
        

        //音乐音效初始化
        Global.AudioManager = require('../Base/AudioManager');
        Global.AudioManager.init();

        //初始化HTTPAPI网络
        Global.HttpApi = require('../API/HttpAPI');
        
        //系统信息
        Global.SystemInfoModel = require('../Models/SystemInfoModel');

        //初始化图层管理
        Global.LayerManager = require('../Base/LayerManager');
        Global.LayerManager.init();
        
        //用户信息
        Global.UserModel = require('../Models/UserModel');
        Global.UserModel.init({});

        this.getSystemInfo();
    },
    getSystemInfo(){
        var _this = this;
        Global.HttpApi.getSystemInfo(function(result){
            Global.SystemInfoModel.init(result.data)
            _this.loadRes();
        });
    },

    loadRes(){
        var _this = this;
        _this.ProgressBar.node.active = true;
        cc.resources.loadDir('/', function (num, totalNum, item) {
            var pge = num/totalNum;
            _this.ProgressBar.progress = pge;
            _this.label.string = `资源加载中...${Math.round(pge*100)}%`;
        }.bind(this),function(err, assets) {
            _this.getUserInfo();
        }.bind(this));
    },

    getUserInfo(){
        if (!cc.sys.localStorage.getItem('token')){
            cc.director.loadScene("loginScence");
            return;
        }
        Global.HttpApi.getUserInfo(function(result){
            Global.NoticeManager.emitMessage('UpdateToken',result.data.userinfo.token);
            Global.NoticeManager.emitMessage('UpdateUserInfoPush',result.data.userinfo.token);
            showWaiting(true);
            setTimeout(function(){
                cc.director.loadScene("hallScence");
            },200); 
        })
    }
});
